package databasetest;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.Connection;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import javax.swing.JDesktopPane;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class DatabaseTest extends JFrame {
	//ㅋㅋㅋㅋㅋㅋ 송태진!!!!!
	// You Die!!!
	Insert in;
	Delete del;
	Select sel;
	Update up;
	Connection conn;

	private JDesktopPane contentPane;
	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenuItem mntmNewMenuItem_2;
	private JMenuItem mntmNewMenuItem_3;

	/**
	 * Launch the application.
	 */

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DatabaseTest frame = new DatabaseTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public DatabaseTest() {
		setTitle("디비연동연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 960, 680);
		setJMenuBar(getMenuBar_1());
		contentPane = new JDesktopPane();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		try {
			OracleJdbc o = new OracleJdbc();
			conn = o.conn;
		} catch (Exception e) {
		}
	}
	
	public void openSelect(){
		if (up == null) {
			up = new Update(conn,this);
			contentPane.add(up);
		}
		if(del == null){
			del = new Delete(conn,this);
			contentPane.add(del);
		}
		if (sel == null) {
			sel = new Select(conn, this);
			contentPane.add(sel);
		}

		sel.setVisible(true);
		sel.toFront();
	}
	
	public void openInsert(){
		if (in == null) {
			in = new Insert(conn, this);
			contentPane.add(in);
		}
		in.setVisible(true);
		in.toFront();
	}
	
	public void openUpdate(){
		if (up == null) {
			up = new Update(conn, this);
			contentPane.add(up);
		}
		up.setVisible(true);
		up.toFront();		
	}
	
	public void openDelete(){
		if (del == null) {
			del = new Delete(conn, this);
			contentPane.add(del);
		}
		del.setVisible(true);
		del.toFront();
	}
	
	
	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnNewMenu());
		}
		return menuBar;
	}

	private JMenu getMnNewMenu() {
		if (mnNewMenu == null) {
			mnNewMenu = new JMenu("New menu");
			mnNewMenu.add(getMntmNewMenuItem_1());
			mnNewMenu.add(getMntmNewMenuItem());
			mnNewMenu.add(getMntmNewMenuItem_2());
			mnNewMenu.add(getMntmNewMenuItem_3());
		}
		return mnNewMenu;
	}

	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("수정");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
	
					openUpdate();
				}
			});
		}
		return mntmNewMenuItem;
	}

	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("입력");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					openInsert();
				}
			});
		}
		return mntmNewMenuItem_1;
	}

	private JMenuItem getMntmNewMenuItem_2() {
		if (mntmNewMenuItem_2 == null) {
			mntmNewMenuItem_2 = new JMenuItem("삭제");
			mntmNewMenuItem_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					openDelete();
				}
			});
		}
		return mntmNewMenuItem_2;
	}

	private JMenuItem getMntmNewMenuItem_3() {
		if (mntmNewMenuItem_3 == null) {
			mntmNewMenuItem_3 = new JMenuItem("조회");
			mntmNewMenuItem_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {			

					openSelect();
				}
			});
		}
		return mntmNewMenuItem_3;
	}
}
